# -*- coding: utf-8 -*-
# @Author：fanmingxiang@touty.io
# @Date  ：2022/form_case/27 15:23
import logging.handlers
import os
import time

from config import BASE_PATH


class GetLog:

    __logger = None

    @classmethod
    def get_logger(cls):
        if cls.__logger is None:
            cls.__logger = logging.getLogger()
            cls.__logger.setLevel(logging.INFO)
            log_name = time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'
            filepath = BASE_PATH + os.sep + 'log' + os.sep + log_name
            th = logging.handlers.TimedRotatingFileHandler(filename=filepath,
                                                           when='midnight',
                                                           interval=1,
                                                           backupCount=0,
                                                           encoding='utf-8')
            fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
            fm = logging.Formatter(fmt)
            th.setFormatter(fm)
            cls.__logger.addHandler(th)
        return cls.__logger
